<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');
/**
 *	Upload Controller
 *
 *	After the upload of a kml file. The kml file will be parsed and add the rout to the database.
 *
 *	NEXT STEP
 *	Merge this Controller with the Route Controller, to add a uploaded route as a new route
 */
class Upload extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	}

	function index()
	{
		$this->load->view('upload_form', array('error' => ' ' ));
	}

	function do_upload()
	{
		// TODO: Problem with the upload path. Get following message -> 'The upload path does not appear to be valid.'
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'xml|kml';
		$config['max_size']	= '2048';
		
		$this->load->library('upload', $config);

		$field_name = "userfile";
		if (!$this->upload->do_upload($field_name))
		{
			$error = array('error' => $this->upload->display_errors());

			$this->load->view('upload_form', $error);
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());

			// Parsing of the uploaded file			
			if(file_exists($data[full_path]))
			{
				$xml = simplexml_load_file($data[full_path]);
				
				foreach($xml->Document[0]->Placemark as $placemark)
				{
					if($placemark->MultiGeometry != null)
					{
						$tmpCoordinates = $placemark->MultiGeoemtry->LineString->coordinates; // TODO Make it better
					
						$coordinatesArray = explode("\n", $tmpCoordinates);
						$relevantCoordinatesArray = array();

						for($index=0; $index<sizeof($coordinatesArray); $index++)
						{	
							$tempArray = explode(",", $coordinatesArray[$index]);

							$relevantCoordinatesArray[] = $tempArray[1];
							$relevantCoordinatesArray[] = $tempArray[0];
						}

						// Build the data structure for the coming JSON object
						$coordinate = array();

						for($index=0; $index<sizeof($relevantCoordinatesArray); $index++)
						{
							if ($index % 2 == 0) 
							{
								$arrayIndex = $index; 
								$coordinate[] = array("lat" => $relevantCoordinatesArray[$arrayIndex], "lng" => $relevantCoordinatesArray[$arrayIndex+1]);
							}
						}

						$route = array("route" => array("coordinate" => $coordinate));

						// Send a JSON Object to the database
						newRoute(json_encode($route));
					}
				}
			}
			else
			{
				// Case if upload fails
				$this->load->view('upload_failed');
			}			
		}
	}
	
	// Add a new route to the database
	public function newRoute($param)
	{
		$this->load->view('header');

		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'route';
		$this->load->view('menu',$menu);
		$this->load->view('route_newroute');
		$this->load->view('footer');

 		$routeFile = trim($param);
 		$ownerID=22;
		
		$name = "tesMAp";
		
 		$difficulty = 2;
 		$ground = "steinig";
 		$length = 1000;
 		$public = 1;
 		
 		$this->routes_model->addRoute($ownerID,$name,$routeFile,$difficulty,$ground,$length,$public);
	}

}
?>